<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class JpushController extends BaseController {

	public function index() {
		$this->display();
	}

	public function push_msg(){
		if ($_POST != array_filter($_POST)) {
			$this->error('选项不能为空');
		}
		$jpush_type = $_POST['jpush_type'];
		$jpush_id = $_POST['jpush_id'];
		$jpush_content = $_POST['jpush_content'];
		switch ($jpush_type) {
			case 1:      //考生
				$data['user_id'] = array('in',$jpush_id);
				$user_ids = M('user')->field('user_id,user_nickname')->where($data)->select();
				$str = '';
				foreach ($user_ids as $v) {
					if($str == ''){
						$str = 'user'.$v['user_id'];
					}else{
						$str .= ',user'.$v['user_id'];
					}
					$condition['message_user_id'] = $v['user_id'];
					$condition['message_client'] = 1;
					$condition['message_type'] = 1;
					$condition['message_time'] = time();
					$condition['message_title'] = $v['user_nickname'].',您有一条新推送消息';
					$condition['message_content'] = $jpush_content;
					M('message')->add($condition);
				}
				$user_id_arr = explode(',', $str);
				$receiver_value = array('tag'=>$user_id_arr);
				push($receiver_value,$jpush_content,C(JPUSH_USER),1);
				$this->success('发送推送完成');
				break;
			case 2:      //考官
				$data['user_id'] = array('in',$jpush_id);
				$examiner_ids = M('user')->field('user_id,user_nickname')->where($data)->select();
				$str = '';
				foreach ($examiner_ids as $v) {
					if($str == ''){
						$str = 'examiner'.$v['user_id'];
					}else{
						$str .= ',examiner'.$v['user_id'];
					}
					$condition['message_user_id'] = $v['user_id'];
					$condition['message_client'] = 2;
					$condition['message_type'] = 1;
					$condition['message_time'] = time();
					$condition['message_title'] = $v['user_nickname'].',您有一条新推送消息';
					$condition['message_content'] = $jpush_content;
					M('message')->add($condition);
				}
				$examiner_id_arr = explode(',', $str);
				$receiver_value = array('tag'=>$examiner_id_arr);
				push($receiver_value,$jpush_content,C(JPUSH_EXAMINER),1);
				$this->success('发送推送完成');
				break;
			default:
				$this->error('必须选择推送对象');
				break;
		}
	}

}